package com.bawei.hgshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.bawei.hgshop.entity.Brand;
import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.entity.Spu;
import com.bawei.hgshop.entity.SpuVo;
import com.bawei.hgshop.service.BrandService;
import com.bawei.hgshop.service.SpuService;
import com.bawei.hgshop.utils.HgFile;
import com.github.pagehelper.PageInfo;

@RequestMapping("spu")
@Controller
public class SpuController {
	
	@Reference
	SpuService spuService;
	
	@Reference
	BrandService brandService; 
	
	
	/**
	 * 列表
	 * @param request
	 * @param spuVo
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest request, SpuVo spuVo) {
		System.out.println("spuVo" + spuVo);
		PageInfo<Spu> pageInfo  = spuService.list(spuVo);
		//回传页面结果数据
		request.setAttribute("pageInfo", pageInfo);
		// 用于查询条件回显
		request.setAttribute("spuVo", spuVo);
		return "spu/list";
	}
	
	/**
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("toadd")
	public String toAdd(HttpServletRequest request) {
		// 获取品牌下拉框
		List<Brand> brands = brandService.listAll();
		request.setAttribute("brands", brands);
		
		return "spu/add";
		
	}
	
	/**
	 * 跳转到修改的页面
	 * @param request
	 * @return
	 */
	@RequestMapping("toaupdate")
	public String toUpdate(HttpServletRequest request,int id) {
		// 获取品牌下拉框
		List<Brand> brands = brandService.listAll();
		request.setAttribute("brands", brands);
		// 获取要修改的数据
		Spu spu = spuService.getById(id);
		request.setAttribute("spu", spu);
		
		return "spu/update";
		
	}
	
	/**
	 *  添加
	 * @param spu
	 * @param file
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public MsgResult add(Spu spu,MultipartFile file) {
		
		// 上传图片
		String filePath = HgFile.upload(file);
		// 设置图片路径
		spu.setSmallPic(filePath);
		int n = spuService.add(spu);
		if(n>0)
			return new MsgResult(0, "", null);
		
		return new MsgResult(1, "添加失败", null);
		
	}
	
	/**
	 * 跳转到修改的页面
	 * @param request
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public MsgResult update(HttpServletRequest request,Spu spu,MultipartFile file) {

		// 上传图片
		String filePath = HgFile.upload(file);
		// 设置图片路径
		spu.setSmallPic(filePath);
		int n = spuService.update(spu);
		if(n>0)
			return new MsgResult(0, "", null);
		
		return new MsgResult(1, "修改失败", null);
		
	}
	
	
	/**
	 *  批量删除
	 * @return
	 */
	@RequestMapping("delbatch")
	@ResponseBody
	public MsgResult delBatch(HttpServletRequest request,@RequestParam(value = "ids[]") int[] ids) {
		int n  = spuService.delBatch(ids);
		//删除成功
		if(n>0)
			return new MsgResult(0, "", null);
		else
			return new MsgResult(2, "删除失败，请稍后再试", null);
	}
	
	
	
	

}
